﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SmartDeviceProject1
{
    public partial class Form1 : Form
    {
        private string fileSelected;
        private Alarm alarm;

        public Form1()
        {
            InitializeComponent();

            if (Alarm.Saved)
            {
                alarm = Alarm.Load();
                if (alarm != null)
                {
                    if (alarm.Time < DateTime.Now)
                        alarm.Off();
                    else
                        updateAlarmState(alarm);
                }              
            }

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (alarm == null || alarm.State == AlarmState.off)
            {

                if (!Validate())
                    return;

                this.alarm = new Alarm(dateTimePicker1.Value, fileSelected);

                updateAlarmState(alarm);
            }
            else
            {
                alarm.Off();

                updateAlarmState(alarm);  
            }
            
        }

        private bool Validate()
        {
            if (!System.IO.File.Exists(fileSelected))
            {
                MessageBox.Show("A valid playlist has not been selected. Please choose a playlist that you would like to wake up to.", "Invalid Playlist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return false;
            }
            return true;
        }


        private void updateAlarmState(Alarm alarm)
        {
            if (alarm.State == AlarmState.on)
            {
                textBox1.Text = alarm.PlayFile;
                dateTimePicker1.Text = alarm.Time.ToString("t");
                labelStatus.Text = "Alarm: " + alarm.Time.ToString("f");
                dateTimePicker1.Enabled = false;
                textBox1.Enabled = false;
                button1.Enabled = false;
                menuItem1.Text = "Turn alarm off";
                menuItem3.Enabled = false;
            }
            else if (alarm.State == AlarmState.off)
            {
                dateTimePicker1.Enabled = true;
                textBox1.Enabled = true;
                button1.Enabled = true;

                labelStatus.Text = "An alarm has not been set.";

                menuItem1.Text = "Turn alarm on";
                menuItem3.Enabled = true;
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                fileSelected = openFileDialog1.FileName;
                textBox1.Text = fileSelected;
            }      


        }

        private void menuItem2_Click(object sender, EventArgs e)
        {

        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            new About().Show();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            if (!Validate())
                return;

            System.Diagnostics.Debug.Assert(alarm == null || alarm.State == AlarmState.off, "Alarm should not be on if testing is clickable");
            new Alarm(DateTime.Now.AddSeconds(2), fileSelected);
        }
    }
}